﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml;

namespace iReaper
{
    public class DownloadUrlManager
    {
        static UrlDict g_dict;

        public static void Init()
        {
            OriginalContentManager.Current.OnXmlSynchorized += new EventHandler(OnXmlSynchorized);

            LoadDict();
        }

        private static void OnXmlSynchorized(object sender, EventArgs e)
        {
            LoadDict();
        }

        private static void LoadDict()
        {
            UrlDict dict = new UrlDict();
            dict.LoadDict(OriginalContentManager.Current.Xml893);

            g_dict = dict;
        }

        public static UrlDict Dict
        {
            get { return g_dict; }
        }
    }

    public class CourseUrlItem
    {
        private Dictionary<string, string> m_urlTable;

        public CourseUrlItem()
        {
            m_urlTable = new Dictionary<string, string>();
        }

        public string LoadUrlsFromXml(XmlNode xml)
        {
            ParseAndAddItem("89304", "slides", xml);
            ParseAndAddItem("89305", "video", xml);
            ParseAndAddItem("89306", "code", xml);
            ParseAndAddItem("89307", "qa", xml);
            ParseAndAddItem("89310", "wmv", xml);
            ParseAndAddItem("89311", "mp4", xml);
            ParseAndAddItem("89312", "mp3", xml);

            XmlNode nID = xml.SelectSingleNode("newsID");
            if (nID == null)
            {
                return null;
            }
            return nID.InnerText;
        }

        public string LoadUrlsFromXml(string Xml)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(Xml);

            return LoadUrlsFromXml(xmldoc);
        }

        private void ParseAndAddItem(string id, string name, XmlNode xmlNode)
        {
            XmlNode node = xmlNode.SelectSingleNode(string.Format(".//a[@attCatID='{0}']/@attValue", id));
            if (node != null && !string.IsNullOrEmpty(node.Value.Trim()))
            {
                m_urlTable.Add(name, node.Value);
            }
        }

        public string this[string type]
        {
            get
            {
                if (m_urlTable.ContainsKey(type))
                {
                    return m_urlTable[type];
                }
                else
                {
                    return null;
                }
            }
        }
    }

    public class UrlDict
    {
        Dictionary<string, CourseUrlItem> m_Dict;

        public UrlDict()
        {
            m_Dict = new Dictionary<string, CourseUrlItem>();
        }

        public string GetUrl(string id, string type)
        {
            // Query table
            if (m_Dict.ContainsKey(id))
            {
                return m_Dict[id][type];
            }
            return null;
        }

        public void LoadDict(XmlDocument xmlOf893)
        {
            if (null == xmlOf893)
            {
                return;
            }
            XmlNodeList nRList = xmlOf893.SelectNodes("//R");

            Dictionary<string, CourseUrlItem> dict = new Dictionary<string, CourseUrlItem>(nRList.Count);
            
            
            for (int i = 0;i < nRList.Count;i++)
            {
                XmlNode nR = nRList[i];
                CourseUrlItem item = new CourseUrlItem();
                string id = item.LoadUrlsFromXml(nR);
                if (null != id && !dict.ContainsKey(id))
                {
                    dict.Add(id, item);
                }
                
            }
            m_Dict = dict;
        }
    }
}
